package com.ruoyi.wms.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.id.IdGenarator;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.domain.WmsReceiptOrderDetail;
import com.ruoyi.wms.domain.WmsShipmentOrderDetail;
import com.ruoyi.wms.mapper.WmsInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.mapper.WmsReceiptOrderMapper;
import com.ruoyi.wms.domain.WmsReceiptOrder;
import com.ruoyi.wms.service.IWmsReceiptOrderService;

/**
 * 入库单Service业务层处理
 *
 * @author zmm
 * @date 2023-01-30
 */
@Service
public class WmsReceiptOrderServiceImpl implements IWmsReceiptOrderService {
    @Autowired
    private WmsReceiptOrderMapper wmsReceiptOrderMapper;
    @Autowired
    private WmsInventoryMapper inventoryMapper;


    /**
     * 查询入库单
     *
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public WmsReceiptOrder selectWmsReceiptOrderById(Long id) {
        return wmsReceiptOrderMapper.selectWmsReceiptOrderById(id);
    }

    /**
     * 查询入库单列表
     *
     * @param wmsReceiptOrder 入库单
     * @return 入库单
     */
    @Override
    public List<WmsReceiptOrder> selectWmsReceiptOrderList(WmsReceiptOrder wmsReceiptOrder) {
        return wmsReceiptOrderMapper.selectWmsReceiptOrderList(wmsReceiptOrder);
    }

    /**
     * 新增入库单
     *
     * @param wmsReceiptOrder 入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsReceiptOrder(WmsReceiptOrder wmsReceiptOrder) {
        List<WmsReceiptOrderDetail> wmsReceiptOrderDetailList = wmsReceiptOrder.getWmsReceiptOrderDetailList();
        if (CollectionUtil.isEmpty(wmsReceiptOrderDetailList)) {
            throw new RuntimeException("请填写物料明细");
        }
        for (WmsReceiptOrderDetail wmsReceiptOrderDetail : wmsReceiptOrderDetailList) {
            BigDecimal quantity = BigDecimal.ZERO;
            BigDecimal realQuantity = wmsReceiptOrderDetail.getRealQuantity();
            //库存操作 先查询 同仓库 库区 货架 物料 有没有库存
            WmsInventory wmsInventory = new WmsInventory();
            wmsInventory.setAreaId(wmsReceiptOrderDetail.getAreaId());
            wmsInventory.setItemId(wmsReceiptOrderDetail.getItemId());
            wmsInventory.setWarehouseId(wmsReceiptOrderDetail.getWarehouseId());
            wmsInventory.setRackId(wmsReceiptOrderDetail.getRackId());
            List<WmsInventory> wmsInventories = inventoryMapper.selectWmsInventoryList(wmsInventory);
            if (CollectionUtil.isEmpty(wmsInventories)) {
                inventoryMapper.insertWmsInventory(wmsInventory);
            } else {
                wmsInventory = wmsInventories.get(0);
                quantity = wmsInventory.getQuantity();
            }
            wmsInventory.setQuantity(quantity.add(realQuantity));
            inventoryMapper.updateWmsInventory(wmsInventory);
        }


        wmsReceiptOrder.setCreateTime(DateUtils.getNowDate());
        int rows = wmsReceiptOrderMapper.insertWmsReceiptOrder(wmsReceiptOrder);
        insertWmsReceiptOrderDetail(wmsReceiptOrder);
        return rows;
    }

    /**
     * 修改入库单
     *
     * @param wmsReceiptOrder 入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsReceiptOrder(WmsReceiptOrder wmsReceiptOrder) {
        wmsReceiptOrder.setUpdateTime(DateUtils.getNowDate());
        wmsReceiptOrderMapper.deleteWmsReceiptOrderDetailByReceiptOrderId(wmsReceiptOrder.getId());
        insertWmsReceiptOrderDetail(wmsReceiptOrder);
        return wmsReceiptOrderMapper.updateWmsReceiptOrder(wmsReceiptOrder);
    }

    /**
     * 批量删除入库单
     *
     * @param ids 需要删除的入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsReceiptOrderByIds(Long[] ids) {
        //进行出库操作
        for (Long id : ids) {
            WmsReceiptOrder wmsReceiptOrder = wmsReceiptOrderMapper.selectWmsReceiptOrderById(id);
            List<WmsReceiptOrderDetail> wmsReceiptOrderDetailList = wmsReceiptOrder.getWmsReceiptOrderDetailList();
            if (CollectionUtil.isEmpty(wmsReceiptOrderDetailList)){
                throw  new RuntimeException("请填写物料明细");
            }
            //查库存
            for (int i = 0; i < wmsReceiptOrderDetailList.size(); i++) {
                BigDecimal quantity=BigDecimal.ZERO;
                WmsInventory wmsInventory = new WmsInventory();
                wmsInventory.setAreaId(wmsReceiptOrderDetailList.get(i).getAreaId());
                wmsInventory.setItemId(wmsReceiptOrderDetailList.get(i).getItemId());
                wmsInventory.setWarehouseId(wmsReceiptOrderDetailList.get(i).getWarehouseId());
                wmsInventory.setRackId(wmsReceiptOrderDetailList.get(i).getRackId());
                List<WmsInventory> wmsInventories = inventoryMapper.selectWmsInventoryList(wmsInventory);
                if (CollectionUtil.isEmpty(wmsInventories)){
                    throw new RuntimeException("您选择的货架下第"+(i+1)+"个物料无库存,请重新选择");
                }else {
                    wmsInventory=wmsInventories.get(0);
                    quantity=wmsInventory.getQuantity();
                    //判断库存数量
                    if (quantity.compareTo(wmsReceiptOrderDetailList.get(i).getRealQuantity())<0){

                        throw new RuntimeException("您选择的货架下第"+(i+1)+"个物料库存小于出库数量,请重新选择");
                    }
                    wmsInventory.setQuantity(quantity.subtract(wmsReceiptOrderDetailList.get(i).getRealQuantity()));
                    inventoryMapper.updateWmsInventory(wmsInventory);
                }

            }
        }





        wmsReceiptOrderMapper.deleteWmsReceiptOrderDetailByReceiptOrderIds(ids);
        return wmsReceiptOrderMapper.deleteWmsReceiptOrderByIds(ids);
    }

    /**
     * 删除入库单信息
     *
     * @param id 入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsReceiptOrderById(Long id) {
        wmsReceiptOrderMapper.deleteWmsReceiptOrderDetailByReceiptOrderId(id);
        return wmsReceiptOrderMapper.deleteWmsReceiptOrderById(id);
    }

    /**
     * 新增入库单详情信息
     *
     * @param wmsReceiptOrder 入库单对象
     */
    public void insertWmsReceiptOrderDetail(WmsReceiptOrder wmsReceiptOrder) {
        List<WmsReceiptOrderDetail> wmsReceiptOrderDetailList = wmsReceiptOrder.getWmsReceiptOrderDetailList();
        Long id = wmsReceiptOrder.getId();
        if (StringUtils.isNotNull(wmsReceiptOrderDetailList)) {
            List<WmsReceiptOrderDetail> list = new ArrayList<WmsReceiptOrderDetail>();
            for (WmsReceiptOrderDetail wmsReceiptOrderDetail : wmsReceiptOrderDetailList) {
                wmsReceiptOrderDetail.setId(IdGenarator.nextId());
                wmsReceiptOrderDetail.setReceiptOrderId(id);
                wmsReceiptOrderDetail.setCreateTime(wmsReceiptOrder.getCreateTime());
                wmsReceiptOrderDetail.setCreateBy(wmsReceiptOrder.getCreateBy());
                wmsReceiptOrderDetail.setDelFlag(0);
                list.add(wmsReceiptOrderDetail);
            }
            if (list.size() > 0) {
                wmsReceiptOrderMapper.batchWmsReceiptOrderDetail(list);
            }
        }
    }
}
